package
{
	import org.cove.ape.*;
	
	import flash.events.*;
	import flash.utils.Timer;
	import flash.media.Camera;

	public class Model extends EventDispatcher implements IModel
	{
		
		public var car:Car;//pana scriu  getters......
		
 		public var surfaces:Surfaces;
		
		private static var colA:uint = 0x334433;
		private static var colB:uint = 0x3366aa;
		private static var colC:uint = 0xaabbbb;
		private static var colD:uint = 0x6699aa;
		private static var colE:uint = 0x778877;
		
		//used when checking for changes in model
		private var hasChanged:Boolean;
		private var pxOld:Number,pyOld:Number;
		
		public function Model()
		{
			//APEngine.init(1/4);
			//now done global, because
			//APEengine is static anyway

			car = new Car(colC, colE);
			APEngine.addGroup(car);	
			
			surfaces= new Surfaces(colA, colB, colC, colD, colE);
			APEngine.addGroup(surfaces);		
			car.addCollidableList(new Array(surfaces));


		// set timer to update APE Engine
		var engineTimer:Timer = new Timer(10);
		engineTimer.addEventListener("timer", engineRun);
        engineTimer.start();
		}
		
		private function engineRun(evt:Event):void {
			//APEngine.step();
			//the current mechanism that checks if the car has cahnged is kinda flimsy
			if (car.px!=pxOld) hasChanged=true;
			if (car.py!=pyOld) hasChanged=true;
			
			pxOld=car.px;
			pyOld=car.py;
			
			if (hasChanged)
			{
				this.update();
				hasChanged=false;
			} 			        	
		}
		
		private function update():void
		{
			dispatchEvent(new Event(Event.CHANGE));
		}
		
		public function nextCar():void
		{
			//imediat imediat
		}
		
		public function previousCar():void
		{
		}
		
		public function engineLeft():void
		{
			car.speed=-.2;
		}
		
		public function engineRight():void
		{
			car.speed=.2;
		}		
				
		public function engineOutOfGear():void
		{			
			car.speed=0;
		}
		
		public function engineStoppedInGear():void
		{			
		}
		
		public function getCarPosition():int
		{
			return 1;
		}
	
	}
}